package Collecion;

import java.util.ArrayList;
import java.util.List;

public class lianxi1 {
    public static void main(String[] args) {
        Person p1 = new Person("张三", 18, "男");
       Person p2 = new Person("李四", 18, "男");
        Person p3 =  new Person("王五", 18, "男");
        List<Person> list = new ArrayList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        System.out.println(list);
        for (Person p : list) {
            System.out.println(p);
        }

        //查询是否有小五这个人

        boolean result = find(list, "小五");
        if (result) {
            System.out.println("ok");
        } else {
            System.out.println("不存在");
        }


        //将李四这个人的名字修改为李六。。。
        int index=findIndexof(list,"李四");
        if (index<0) {
            System.out.println("查无此人");
        }else {
            Person person= list.get(index);//获得这个人
            //修改名字
            person.setName("李六");
        }
        System.out.println("list="+list);
        //删除张三，删除之前需要查询，如果有张三则删除，如果没有，输出查无此人
        index=findIndexof(list,"张三");
        if (index<0){
            System.out.println("查无此人");
        }else {
            list.remove(index);
        }
        System.out.println(list);
    }

    private static  int  findIndexof(List<Person> list, String name) {
        int i=0;
        for (i=0;i<list.size();i++){
            if (list.get(i).getName("李四").equals(name)){
                break;
            }
        }
        if (i==list.size()){
            return -1;
        }else {
            return 1;
        }
    }


    private static  boolean find(List<Person> list, String name){
        int i=0;
        for (i=0;i<list.size();i++){
            if (list.get(i).getName("李四").equals(name)){
                break;
            }
        }
        if (i==list.size()){
            return false;
        }else {
            return true;
        }
    }


    static class Person {
        private String name;
        private int age;
        private String sex;

        public Person() {
        }

        public Person(String name, int age, String sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }

        public String getName(String 李六) {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", sex='" + sex + '\'' +
                    '}';
        }



    }
}
